<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="HandheldFriendly" content="true">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript"></script>
<script type="text/javascript">

var search = window.location.search.replace("?", "");
search = decodeURIComponent(search).split("&");
var query = {};
var item;
while(search.length) {
    item = search.pop();
    item = item.split("=");
    query[item[0]] = item[1];
}

var envHost;
var entId;
var loHost = query.type !== "lo" ? window.location.host : "31614.terran.wxpai.cn";
loHost = loHost.split(".");
entId = query.ent || query.task_id.split("-")[0];

if (!entId) {
    entId = loHost.shift();
} else {
    loHost.shift();
}

envHost = loHost.join(".");
// gateway.sandbox.terran.wxpai.cn
var biServer = "//gateway." + envHost;

(function(win, doc){

    var script = document.createElement("script");
    script.src = "//collect."+ envHost +"/sdk/activity?task_id=" + query.task_id;
    script.async = true;
    script.defer = true;
    script.type = "text/javascript";
    doc.head.appendChild(script);
    script.onload = script.onerror = function() {
        script = null;
    };
})(window, document);
</script>
<!-- <link rel="stylesheet" type="text/css" href="css/normalize.css?__inline" /> -->
<!-- <link rel="stylesheet" type="text/css" href="css/common.css?__inline" /> -->
<title>H5 监测测试</title>
</head>
<body>
<div data-click-page="click">
    <div  data-click-areaindex="1">
        <ul>
            <li><a data-click-btn_name="baidu" href="http://www.baidu.com">baidu</a></li>
            <li><a data-click-btn_name="google" href="http://www.google.com">google</a></li>
            <li><a data-click-btn_name="wxpai" href="./pv.html">wxpai</a></li>
            <li><a data-click-btn_name="sandbox" target="_blank" href="http://sandbox.terran.wxpai.cn/">sandbox</a></li>
        </ul>
    </div>
</div>
<div data-click-page="click">
    <div  data-click-areaindex="1">
        <p data-click-btn_name="normal">非超链接</p>
    </div>
</div>


<button type="button" name="button" onclick="sendUser()">
    上传用户
</button>


<script type="text/javascript">
function sendUser() {
    var user = {
        "subscribe": 1,
        "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
        "nickname": "BangBangBang",
        "sex": 1,
        "language": "zh_CN",
        "city": "广州",
        "province": "广东",
        "country": "中国",
        "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
        "subscribe_time": 1382694957,
        "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL",
        "remark": "",
        "groupid": 0
    };
    var appid = "";
    PI_COLLECT.sendAuthUserInfo(user, appid);
}
(function(){
    var confUrl = biServer + "/front/plat/"+ entId +"/weixin/jsapi_signature?url=" + encodeURIComponent(window.location.href);

    function getCookie(name){
        return document.cookie.replace(new RegExp('.*(?:^|; )' + name + '=([^;]*).*|.*'), '$1')
    }

    /**
     * 类型判断
     * @param  {Object}  obj  待判断数据
     * @param  {String}  type 类型名称
     * @return {Boolean}      判断结果
     */
    function is(obj, type){
        var objType = Object.prototype.toString.call(obj);
        objType = objType.substring(8, objType.length - 1).toLowerCase();
        return objType === type;
    }

    /**
     * 发起 ajax 请求
     * @param  {Object}   data 要发送的数据
     * @param  {String}   type 请求类型
     * @param  {String}   url  请求地址
     * @param  {Function} cb   成功后的回调函数
     * @return {Object}        请求对象
     */
    function send(data, type, url, cb){
        var xhr = new XMLHttpRequest();
        xhr.open(type, url, true);
        xhr.withCredentials = true;
        xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.setRequestHeader("Authorization", "Bearer " + getCookie("jwt"));
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (is(cb, "function")){
                    var data;
                    try {
                        data = JSON.parse(xhr.response);
                    } catch(e) {
                        data = {
                            "success":false
                        };
                    }
                    cb(data);
                }
            }
        };
        xhr.send(
            data ? JSON.stringify(data) : null
        );
        return xhr;
    }

    function shareSuccessHandler(type) {
        var location = window.location
        PI_COLLECT.sendShare(type, {
            "url": encodeURIComponent(window.location.href)
        });
    }

    function init() {
        send(null, "GET", confUrl, function(re){
            var conf = re.result;
            conf.jsApiList = ["onMenuShareTimeline", "onMenuShareAppMessage"];
            conf.debug = true;

            wx.config(conf);
            var sharetitle = "欢迎来到假的积分商城";
            var sharedesc = "欢迎来到今天的积分商城";
            var sharelink =  PI_COLLECT.linkChange(window.location.href);
            var shareimg = "http://cdn.dev.terran.wxpai.cn/upload/sandbox/fd2a411b-1b3b-4932-b1a9-79996a587b52.jpg";

            wx.ready(function () {
                wx.onMenuShareAppMessage({
                    title: sharetitle,
                    desc: sharedesc,
                    link: sharelink,
                    imgUrl: shareimg,
                    success: function () {
                        shareSuccessHandler("appMessage");
                    }
                });
                wx.onMenuShareTimeline({
                    title: sharetitle,
                    link: sharelink,
                    imgUrl: shareimg,
                    success: function () {
                        shareSuccessHandler("timeline");
                    }
                });
            });
        });
    }

    function checkSDK() {
        setTimeout(function() {
            if (window.PI_COLLECT && window.wx) {
                init();
            } else {
                checkSDK();
            }
        }, 500);
    }

    checkSDK();

})()
</script>
</body>
</html>
